<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller
{
	public function Index()
	{
		$this->Login();
	}
	
	public function view($page, $data = array())
	{
		$this->load->model('UserModel');
		$this->load->library(array('cart', 'table'));
		
		$this->load->view("templates/header");
		$this->load->view("templates/sidebars", array('logged_in' => $this->UserModel->isLoggedIn(), 'cart' => $this->cart->contents()));
		$this->load->view("user/".$page, $data);
		$this->load->view("templates/footer");
	}
	
	public function Login()
	{
		$this->load->library('form_validation');
		$this->load->model('UserModel');
		
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules('username', 'Username', 'required|max_length[80]');
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[80]');
		
		if(!$this->form_validation->run())
		{
			$this->view('login', array('valid_login' => null));
		}
		elseif(!$this->UserModel->isLoginValid($this->input->post('username'), $this->input->post('password')))
		{
			$this->view('login', array('valid_login' => false));
		}
		else
		{
			$this->UserModel->setLoggedIn($this->input->post('username'));
			redirect('/');
		}
	}
	
	public function Register()
	{
		$data['invalid_captcha_text'] = false;
		
		$this->load->library('form_validation');
		$this->load->model('UserModel');
		
		$this->form_validation->set_message('is_unique', 'This %s is already taken.');
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules('username', 'Username', 'required|max_length[80]|is_unique[users.username]');
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[80]');
		$this->form_validation->set_rules('password2', 'Password Confirm', 'required|max_length[80]|matches[password]');
		$this->form_validation->set_rules('captcha', 'Image Word', 'required|max_length[20]');
		
		if(!$this->form_validation->run())
		{
			$this->view('register', $data);
		}
		else
		{
			$expiration = time()-7200; // Two hour limit
			$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
			
			$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ?";
			$binds = array($this->input->post('captcha'), $this->input->ip_address());
			$query = $this->db->query($sql, $binds);
			$row = $query->row();
			
			if ($row->count == 0)
			{
				$data['invalid_captcha_text'] = true;
				$this->view('register', $data);
			}
			else
			{
				$this->UserModel->addUser($this->input->post('username'), $this->input->post('password'));
				$this->UserModel->setLoggedIn($this->input->post('username'));
				
				/*
				$this->load->library('email');
				$this->email->from('general@dvdsales3489589248746.com', 'General');
				$this->email->to('jamesbmx2006@hotmail.co.uk');
				$this->email->subject('Registration Notice');
				$this->email->message('Thank you for registering '.$this->input->post('username').'.');
				$this->email->send();
				*/
				
				$this->view('register_success');
			}
		}
	}
	
	public function Logout()
	{
		$this->load->model('UserModel');
		
		$this->UserModel->setLoggedOut();
		
		redirect('/');
	}
}